package cn.itcast.mq.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * TODO
 * RabbitMQ监听器
 *
 * @Author mr.Css
 * @Date 2025-01-05 11:05
 */
@Component
@Slf4j
public class SpringRabbitListener {
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMsg(String msg) {
        log.info("[简单队列]-监听消息队列simple.queue：{}", msg);
    }

    /**
     * 工作队列模型-消费者1
     *
     * @param msg
     */
    @RabbitListener(queues = "work.queue")
    public void workQueueListener1(String msg) throws InterruptedException {
        log.info("[工作队列1]-监听消息队列work.queue：{}", msg);
        TimeUnit.MILLISECONDS.sleep(10); // 模拟处理消息需要10毫秒

    }

    /**
     * 工作队列模型-消费者2
     *
     * @param msg
     */
    @RabbitListener(queues = "work.queue")
    public void workQueueListener2(String msg) throws InterruptedException {
        log.info("[工作队列2]-监听消息队列work.queue：{}", msg);
        TimeUnit.MILLISECONDS.sleep(50); // 模拟处理消息需要50毫秒
    }

    /**
     * 广播模型-消费者1
     *
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue1")
    public void FanoutExchangeMessage1(String msg) {
        log.info("[Fanout Exchange] 队列1- 监听到消息：{}", msg);
    }

    /**
     * 广播模型-消费者2
     *
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue2")
    public void FanoutExchangeMessage2(String msg) {
        log.info("[Fanout Exchange] 队列2- 监听到消息：{}", msg);
    }

//    /**
//     * 路由模型-消费者1
//     * @param msg
//     */
//    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue1"),
//            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
//            key = {"blue", "red"}))
//    public void directListener1(String msg) {
//        log.info("路由模型-消费者1-消费到消息：{}", msg);
//    }
//
//    /**
//     * 路由模型-消费者2
//     * @param msg
//     */
//    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue2"),
//            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
//            key = {"yellow", "red"}))
//    public void directListener2(String msg) {
//        log.info("路由模型-消费者2-消费到消息：{}", msg);
//    }
    /**
     * 路由模型-消费者1
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {"blue", "red"}))
    public void directListener1(String msg){
        System.out.println("路由模型-消费者1-消费到消息：" + msg);
    }

    /**
     * 路由模型-消费者2
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {"yellow", "red"}))
    public void directListener2(String msg){
        System.out.println("路由模型-消费者2-消费到消息：" + msg);
    }

    /**
     * 主题模型-消费者1
     *    # 表示匹配0个或多个单词
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = {"china.#"}))
    public void topicListener1(String msg){
        System.out.println("主题模型-消费者1-消费到消息：" + msg);
    }

    /**
     * 主题模型-消费者2
     *    * 表示匹配一个单词
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = {"*.news"}))
    public void topicListener2(String msg){
        System.out.println("主题模型-消费者2-消费到消息：" + msg);
    }
}
